Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:24:20+00:00 2026-06-06T10:24:20+00:00

I have the following code: #include <stdio.h> #include <pthread.h> #define THREAD_CNT 10 #define ITER

  • 0

I have the following code:

#include <stdio.h>
#include <pthread.h>
#define THREAD_CNT 10
#define ITER 100
#define PRINT 1

int lock;
unsigned long long int counter;

void spin_lock(int *p) {
    while(!__sync_bool_compare_and_swap(p, 0, 1));
}

void spin_unlock(int volatile *p) {
    asm volatile ("");
    *p = 0;
}

void *exerciser(void *arg) {
    unsigned long long int i;
    int id = (int)arg;
    for(i = 0; i < ITER; i++) {
        spin_lock(&lock);
        counter = counter + 1;
        if(PRINT) {
            printf("%d: Incrementing counter: %llu -> %llu\n", id, counter-1, counter);
        }
        spin_unlock(&lock);
    }
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
    pthread_t thread[THREAD_CNT];
    counter = 0;
    int i;
    for(i = 0; i < THREAD_CNT; i++) {
        pthread_create(&thread[i], NULL, exerciser, (void *) i);
    }
    for(i = 0; i < THREAD_CNT; i++) {
        pthread_join(thread[i], NULL);
    }
    printf("Sum: %llu\n", counter);
    printf("Main: Program completed. Exiting.\n");
    pthread_exit(NULL);
}

When PRINT is defined as 1, I get the correct counter value at the end:

7: Incrementing counter: 996 -> 997
7: Incrementing counter: 997 -> 998
7: Incrementing counter: 998 -> 999
7: Incrementing counter: 999 -> 1000
Sum: 1000
Main: Program completed. Exiting.

If I make PRINT 0, I get the following (multiple runs):

$ ./a.out
Sum: 991
Main: Program completed. Exiting.
$ ./a.out 
Sum: 1000
Main: Program completed. Exiting.
$ ./a.out 
Sum: 962
Main: Program completed. Exiting.
$ ./a.out 
Sum: 938
Main: Program completed. Exiting.

Any insight as to what’s going on? Why is it that when I have the print statement enabled my results are (consistently) correct, but I disable it and my counter doesn’t reach the target value? I’ve used pthread quite a bit but not very experienced with using spinlocks directly.

Any help or feedback is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:24:22+00:00Added an answer on June 6, 2026 at 10:24 am

    Your lock methods aren’t actually doing anything: since arguments are passed by value, you’re not actually testing/setting the value of your lock global. You’re only changing the value of the copy of the variable your function gets.

    If instead your spin_lock/spin_unlock methods took a pointer to the integer (i.e. &lock) to use then your code should work.

    Your load bearing printf probably helps by causing some unintended synchonisation because printf is supposed to be threadsafe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
I have the following code: #include <stdlib.h> #include <stdio.h> #define SIZE 100 int* arr;
We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following code: /* sample.c */ #include<stdio.h> #include<malloc.h> #include<stdlib.h> #includehermes.h #include<string.h> int
I have a problem with the following code #include <stdio.h> int main() /* Tidsberäkning
I have the following code: #include<stdio.h> int main() { int(* a)[10]; //declare a as
I have the following code: #include <stdio.h> template<int A> class Thing { // 5
I have the following code: #include <stdio.h> #include <math.h> int main(void) { printf(%f\n, fmax(1.2,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.