Re: Setting a global variable into a thread

On 08/12/10 14:08, Pavel Vasilyev wrote:
On 08.12.2010 12:55, David Brown wrote:
On 07/12/2010 21:26, Pavel Vasilyev wrote:
On 07.12.2010 23:22, Pavel Vasilyev wrote:
On 07.12.2010 21:28, Bogdan wrote:
I have a global variable (my_var) initialized to zero and from the
main thread another thread is created which only sets the global
variable to one. After creating the secondary thread, the main thread
waits for the global variable to be set to one like this:

while (0 == my_var);

Even if the global variable is set in the secondary thread, the main
thread remains blocked forever in the while loop. If I use instead:

while (0 == my_var)

the main thread detects that the global variable has been set. Could
someone explain what happens in the first case and why the global
variable is not detected when it is set ?


int local_var = 0;

while (0 == local_var)

local_var = my_var;

The same is true in other places where you use this variable.

What's in your "write_block" and "write_unblock" functions/macros?
Just example.

And how is this better than making my_var volatile?
Not work.

My experience with this sort of thing is mostly with small microcontrollers, rather than Linux - most of my Linux programming is in Python rather than C, which handles this sort of thing differently.

Can you tell me /why/ this would not work:

static volatile int my_var;

void threadOne(void) {
while (0 == my_var) ;

void threadTwo(void) {
my_var = 1;

Obviously the first thread will busy-wait in the while loop, which is seldom a good idea. But as long as priorities and/or SMP allow threadTwo to run, it will work.



There are times when rtos-style locking and synchronisation tools are
important, and times when they are way over-kill. If he wanted an rtos
solution, my_var would be a semaphore.


sem_t a, b;
int global = 10;

void *one(void *arg __attribute__((unused))) {

int local;

do {
local = global--;
printf("A = %d\n", local);
} while (local> 0);


void *two(void *arg __attribute__((unused))) {

int local;

do {
local = global--;
printf("B = %d\n",local);
} while (local> 0);


int main(void) {

pthread_t pth[2];
pthread_attr_t pattr;

pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);

sem_init(&a, 0, 0);
sem_init(&b, 0, 1);

pthread_create(&pth[0],&pattr,&one, NULL);
pthread_create(&pth[1],&pattr,&two, NULL);


sem_destroy (&a);
sem_destroy (&b);

return 0;


Relevant Pages

  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... You can't use it to sort a list of ints where the int is ... You can use it to sort a list of pointers to any type. ...
  • Re: qsort returning index
    ... However if you have salaries in a parallel array, ... just to do the sort. ... int *qsortindex(void *buf, int N, int size, int (*compfunc)(const void ...
  • Re: resize JTable
    ... >able to easily sort the data, from a user point of view. ... public static final int DESCENDING = -1; ... private Comparator comparator; ... * @param model OrderedTableModel to be kept deduped/sorted. ...
  • Re: problem with array
    ... > different sort algorythms. ... > the sort the array elements are compared and swapped. ... void selectionsort(void *base, size_t n, size_t size, ... void PrintArrays(int *a, int **pa,size_t n); ...
  • Java VM 1.6.0 Sorting Collections
    ... I have been having some trouble with the performance of Java. ... public static void main ... final int vecSize = 1000000; ... Note I am only timing the sort function. ...