I need some help in using pthreads (win32 version) with vc++.
I have written some simple code to test the accuracy of pthreads in means of clock times or ticks then tried some combinations of sleep() function to make my program work in synch within the threads but nope it did not work or it works but with weird outputs which i will explain briefly here.
Following is my 2 simple threads one is a timer function which increases a variable called timer by one every cpu tick (by sleep(5)) the other is just used for printing out the value of timer variable when the value becomes 1 modulus 100. Here is the simple code :
unsigned int timer = 0;
void *tickThread(void *null)
{
for (;
{
if ((timer % 100) == 1)
{
printf("\n in tickThread and in if and Timer is : %u",timer);
}
}
return (void*)0;
}
void *timerThread(void *null)
{
for(;
{
Sleep(5);
timer++;
}
}
main ()
{
// create/initiate both threads
// join both threads
}
in main we first initiate both threads then we join both in the same time.
in this simple example we have a problem that is :
When both of the threads are initiated the variable timer is incrementing normaly but when we try to see the value of "timer" (our variable) from the other thread by using a conditional-if , some different problems may occur depending on the usage or place of sleep() function .
1-The problem with original code : Since the printf function is too slow to print the value of variable in the tickThread, the dummy cpu ignores the printf function and nothing is printed on to the screen. One thing is very important to note here that is ; the program executes and gets inside the if condinitional of tickThread normaly. Only the printf function is ommitted.
2-If we add Sleep(1) or Sleep(5) line to the if conditional of tickThread before the printf function then the output becomes even more bizarre that is : everytime when timer is equal to 1 modulus 100 the printf function in the if conditional of tickthread is executed but in a weird way. The program prints
a. in tickThread and in if and Timer is :101
in tickThread and in if and Timer is :102, if we had used Sleep(1) in the conditional or
b. in tickThread and in if and Timer is : 102 , if we’d used Sleep(5) at that line
3-The following (strange) output occurs if we add Sleep(5) line outside of and just before if condition in the infinite for loop of the thread function “tickThread”
in tickThread and in if and Timer is : 1
in tickThread and in if and Timer is : 101
in tickThread and in if and Timer is : 201
in tickThread and in if and Timer is : 301
in tickThread and in if and Timer is : 401
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 601
in tickThread and in if and Timer is : 701
in tickThread and in if and Timer is : 801
in tickThread and in if and Timer is : 901
in tickThread and in if and Timer is : 1001
in tickThread and in if and Timer is : 1201
in tickThread and in if and Timer is : 1301
-----
and this erroneous output may change in every execution of program.
Ok you gurus out there ; come figure!
--------------------------------------------
"Advances are made by answering questions; discoveries are made by answering questions
I have written some simple code to test the accuracy of pthreads in means of clock times or ticks then tried some combinations of sleep() function to make my program work in synch within the threads but nope it did not work or it works but with weird outputs which i will explain briefly here.
Following is my 2 simple threads one is a timer function which increases a variable called timer by one every cpu tick (by sleep(5)) the other is just used for printing out the value of timer variable when the value becomes 1 modulus 100. Here is the simple code :
unsigned int timer = 0;
void *tickThread(void *null)
{
for (;
{
if ((timer % 100) == 1)
{
printf("\n in tickThread and in if and Timer is : %u",timer);
}
}
return (void*)0;
}
void *timerThread(void *null)
{
for(;
{
Sleep(5);
timer++;
}
}
main ()
{
// create/initiate both threads
// join both threads
}
in main we first initiate both threads then we join both in the same time.
in this simple example we have a problem that is :
When both of the threads are initiated the variable timer is incrementing normaly but when we try to see the value of "timer" (our variable) from the other thread by using a conditional-if , some different problems may occur depending on the usage or place of sleep() function .
1-The problem with original code : Since the printf function is too slow to print the value of variable in the tickThread, the dummy cpu ignores the printf function and nothing is printed on to the screen. One thing is very important to note here that is ; the program executes and gets inside the if condinitional of tickThread normaly. Only the printf function is ommitted.
2-If we add Sleep(1) or Sleep(5) line to the if conditional of tickThread before the printf function then the output becomes even more bizarre that is : everytime when timer is equal to 1 modulus 100 the printf function in the if conditional of tickthread is executed but in a weird way. The program prints
a. in tickThread and in if and Timer is :101
in tickThread and in if and Timer is :102, if we had used Sleep(1) in the conditional or
b. in tickThread and in if and Timer is : 102 , if we’d used Sleep(5) at that line
3-The following (strange) output occurs if we add Sleep(5) line outside of and just before if condition in the infinite for loop of the thread function “tickThread”
in tickThread and in if and Timer is : 1
in tickThread and in if and Timer is : 101
in tickThread and in if and Timer is : 201
in tickThread and in if and Timer is : 301
in tickThread and in if and Timer is : 401
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 601
in tickThread and in if and Timer is : 701
in tickThread and in if and Timer is : 801
in tickThread and in if and Timer is : 901
in tickThread and in if and Timer is : 1001
in tickThread and in if and Timer is : 1201
in tickThread and in if and Timer is : 1301
-----
and this erroneous output may change in every execution of program.
Ok you gurus out there ; come figure!
--------------------------------------------
"Advances are made by answering questions; discoveries are made by answering questions