http://kmlinux/~culik/wiki
#include "windows.h"
HANDLE s;
bool stop = false;
unsigned long __stdcall f (void * p)
{
int n = (int) p;
while (! stop)
{
WaitForSingleObject (s, INFINITE);
printf ("Kriticka sekce %d \n", n);
ReleaseSemaphore (s, 1, NULL);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
s = CreateSemaphore
(NULL, // security attr
1, // init
1, // max
NULL // name
);
for (int i = 1; i < 3; i ++)
CreateThread
(NULL, 0, f, (void *) i, 0, NULL);
char c;
scanf ("%c", &c);
stop = true;
return 0;
}
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int fce (void * data)
{
while (true)
{
cout << "moje vlakno" << endl;
Sleep (100);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE t;
t = CreateThread
(NULL, // security attr
0, // default stack size
(LPTHREAD_START_ROUTINE) fce,
NULL, // func param
0, // flags
NULL); // ref thread id
cout << "press enter";
char c;
cin >> c;
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
int vlakno1 (void * data)
{
while (true)
{
cout << "vlakno 1" << endl;
Sleep (100);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE t1;
t1 = CreateThread
(NULL, // security attr
0, // default stack size
(LPTHREAD_START_ROUTINE) vlakno1, // thread func
NULL, // func param
0, // creation flags
NULL); // "var" thread id
cout << "press enter" << endl;
char c;
cin >> c;
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
int vlakno1 (void * data)
{
int id = (int) data;
while (true)
{
cout << "vlakno " << id << endl;
Sleep (100);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 1; i < 8; i++)
{
HANDLE t1;
t1 = CreateThread
(NULL, // security attr
0, // default stack size
(LPTHREAD_START_ROUTINE) vlakno1, // thread func
(void *) i, // func param
0, // creation flags
NULL); // "var" thread id
}
cout << "press enter" << endl;
char c;
cin >> c;
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
int vlakno1 (void * data)
{
int id = (int) data;
while (true)
{
cout << "vlakno ";
Sleep (100);
cout << id << endl;
Sleep (100);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
// for (int i = 1; i < 8; i++)
// {
HANDLE t1;
t1 = CreateThread
(NULL, // security attr
0, // default stack size
(LPTHREAD_START_ROUTINE) vlakno1, // thread func
(void *) 1, // func param
0, // CREATE_SUSPENDED, // creation flags
NULL); // "var" thread id
// }
char c;
cout << "press enter" << endl; cin >> c;
SuspendThread (t1);
cout << "press enter" << endl; cin >> c;
ResumeThread (t1);
cout << "press enter" << endl; cin >> c;
TerminateThread (t1, 0);
cout << "press enter" << endl; cin >> c;
return 0;
}