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 8473707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:25:34+00:00 2026-06-10T17:25:34+00:00

I got a tough problem about Pthread I cannot figure it out. I use

  • 0

I got a tough problem about Pthread I cannot figure it out. I use mutex to synchronize the subthread and main thread. But when I lock subFinished[i] in the main thread, it just cannot get it locked, and stuck there. It sometimes went well, especially when I insert some std output into the code. But mostly, it will be stuck when trying to lock mutex subFinished[i].

Is there any unpredictable things will happen in the way I synchronize threads?

Homework due tomorrow. Many thx!

Code is as below in C.

#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <time.h>
#include <stdlib.h>

#include <gtk/gtk.h>

#include <pthread.h>

#define MAP_SIZE 10 

#define WALL_TEMP 20
#define FIREPLACE_TEMP 100

#define MAX_THREAD_NUM 64

#define start_time clock_gettime(CLOCK_MONOTONIC, &start);
#define end_time clock_gettime(CLOCK_MONOTONIC, &finish);

pthread_barrier_t diff_barrier;

struct timespec start, finish;

const double SHRESHOLD = 1.0e-2;

GtkWidget *window;
GtkWidget *draw_area;
GdkColor temp_color;

int iter_cnt, iter_times;

int strip_height;
int row_start, row_end;

//double x[MAP_SIZE+2][MAP_SIZE+2], x_new[MAP_SIZE+2][MAP_SIZE+2];

double diff[MAX_THREAD_NUM];

int msgtag=0;

pthread_t threads[MAX_THREAD_NUM];
int start_idx[MAX_THREAD_NUM];
int end_idx[MAX_THREAD_NUM];

int thread_n;

int converged = 0;

pthread_mutex_t subThreadWakeup[MAX_THREAD_NUM];
pthread_mutex_t subFinished[MAX_THREAD_NUM];

double **x_pointer, **x_new_pointer;

double *x, *x_new;
void *thread_work(void *arg){
    int thread_id = *(int*)arg;
    int row_start = start_idx[thread_id] ;
    int row_end = end_idx[thread_id];   
    while(1){
        pthread_mutex_lock(&subThreadWakeup[thread_id]);
        printf("Thread#%d get lock %d\n", thread_id, thread_id);
        if(converged)
            break;
        int i, j;
        for(i=row_start; i<=row_end; i++){
            for(j=1; j<=MAP_SIZE;j++){
                x_new_pointer[i][j] = (x_pointer[i][j-1] + x_pointer[i][j+1] + x_pointer[i-1][j] + x_pointer[i+1][j])/4;
                diff[thread_id] += (x_new_pointer[i][j] - x_pointer[i][j]) * (x_new_pointer[i][j] - x_pointer[i][j]);
            }
        }

        printf("thread#%d finished, but not unlock subFinished[%d].\n", thread_id, thread_id);
        //printf("mutex unlock: %d\n", pthread_mutex_unlock(&subFinished[thread_id]));
//      subFinished[i] = 1;
        pthread_mutex_unlock(&subFinished[thread_id]);
        printf("thread#%d finished, already unlock subFinished[%d].\n", thread_id, thread_id);
    }
    pthread_exit(NULL);
}
#ifdef DISPLAY
static gboolean expose_event_callback(GtkWidget *widget){
    GdkGC *gc = widget->style->fg_gc[GTK_WIDGET_STATE(widget)];
    GdkDrawable *drawable = widget->window;

    int i, j;
    for(i=1; i<=MAP_SIZE; i++){
        for(j=1; j<=MAP_SIZE; j++){
            temp_color.red = (int)(x[i][j])*(0xffff)/(5*20);
            temp_color.green = temp_color.blue = temp_color.red/2;
            gdk_gc_set_rgb_fg_color(gc, &temp_color);
            gdk_draw_point(drawable, gc, j, i);
        }
    }
}
#endif

int main(int argc, char *argv[]){

    int i,j,k;
    int tids[MAX_THREAD_NUM];
    for(i=0; i<MAX_THREAD_NUM; i++)
        tids[i] = i;

    if(argc > 1)
        thread_n = atoi(argv[1]);
    else
        thread_n = 1;

    if(argc > 2)
        iter_times = atoi(argv[2]);
    else
        iter_times = 0;

    printf("%d\n", thread_n + 1);
    // Specify the start_idx and end_idx
    int remainder = MAP_SIZE % thread_n;

    for(i=0; i<thread_n; i++){
        if(i==0) start_idx[i] = 1;
        else start_idx[i] = end_idx[i-1]+1;
        end_idx[i] = start_idx[i] + MAP_SIZE/thread_n + (i<remainder) - 1;
    }

    x = malloc(sizeof(double)*(MAP_SIZE+2)*(MAP_SIZE+2));
    x_new = malloc(sizeof(double)*(MAP_SIZE+2)*(MAP_SIZE+2));
    x_pointer = malloc(sizeof(double*)*(MAP_SIZE+2));
    x_new_pointer = malloc(sizeof(double*)*(MAP_SIZE+2));

    for(i=0; i<MAP_SIZE+2; i++) x_pointer[i] = &x[i*(MAP_SIZE+2)];
    for(i=0; i<MAP_SIZE+2; i++) x_new_pointer[i] = &x_new[i*(MAP_SIZE+2)];
    // Specify x matrix
    for(j=0; j<0.3*MAP_SIZE; j++)
        x_pointer[0][j] = WALL_TEMP;
    for(j=0.7*MAP_SIZE; j<MAP_SIZE+1; j++)
        x_pointer[0][j] = WALL_TEMP;
    for(j=0.3*MAP_SIZE; j<0.7*MAP_SIZE; j++)
        x_pointer[0][j] = FIREPLACE_TEMP;

    for(j=0; j<MAP_SIZE+2; j++)
        x_pointer[MAP_SIZE+1][j]=WALL_TEMP;

    for(i=0; i<MAP_SIZE+2; i++){
        x_pointer[i][0] = WALL_TEMP;
        x_pointer[i][MAP_SIZE+1] = WALL_TEMP;
    }

    // initialize x_new the same way as x
    for(i=0; i<=MAP_SIZE+1; i++)
        for(j=0; j<=MAP_SIZE+1;j ++)
            x_new_pointer[i][j] = x_pointer[i][j];

    for(i=0; i<thread_n; i++){
        pthread_create(&threads[i], NULL, thread_work, &tids[i]);
        printf("create %d\n", tids[i]);
    }

    for(i=0; i<thread_n; i++){
        pthread_mutex_init(&subThreadWakeup[i], NULL);
        pthread_mutex_init(&subFinished[i], NULL);
        pthread_mutex_lock(&subThreadWakeup[i]);
        pthread_mutex_lock(&subFinished[i]);
    }


    double total_diff = 0;
    start_time
    do{
        iter_cnt ++;
        total_diff = 0;     
        memset(diff, 0.0, thread_n * sizeof(double));
        for(i=0; i<thread_n; i++){
            pthread_mutex_unlock(&subThreadWakeup[i]);
        }

        printf("++");
        printf("all wake up.");

        for(i=0; i<thread_n; i++){
            printf("Try get lock %d\n", i);

            pthread_mutex_lock(&subFinished[i]);

            printf("Get Lock %d\n", i);
        }
        printf("all finished.\n");
        fflush(stdout);
        double **tmp_pointer = x_pointer;
        x_pointer = x_new_pointer;
        x_new_pointer = tmp_pointer;

        for(i=0; i<thread_n; i++)
            total_diff += diff[i];
        printf("total_diff: %lf\n", total_diff);
    }while( sqrt(total_diff) >= SHRESHOLD && !(iter_cnt >= iter_times && iter_times));

    converged = 1;

    for(i=0; i<thread_n; i++)
        pthread_mutex_unlock(&subThreadWakeup[i]);

    end_time
    printf("%d %d %lf\n", thread_n, iter_cnt, finish.tv_sec-start.tv_sec + (double)(finish.tv_nsec - start.tv_nsec)/ 1000000000.0);

#ifdef DISPLAY
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request(window, MAP_SIZE, MAP_SIZE);
    gtk_window_set_title(window, "Heat Distribution");

    draw_area = gtk_drawing_area_new();
    gtk_widget_set_size_request(draw_area, MAP_SIZE, MAP_SIZE);
    gtk_container_add(GTK_CONTAINER(window), draw_area);    

    g_signal_connect(G_OBJECT(draw_area), "expose_event", G_CALLBACK(expose_event_callback), NULL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);



    gtk_main();
#endif
    return 0;
}

Using

$ gcc -O2 -o pthread_mutex pthread_mutex.c -lm -lrt -lpthread `pkg-config --cflags --libs gtk+-2.0`

to compile.

  • 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-10T17:25:35+00:00Added an answer on June 10, 2026 at 5:25 pm

    Oh, I made a fatal mistake that I put the pthread_create before mutex_init, which causes a unpredictable result.

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

Sidebar

Related Questions

I know about non-breaking spaces but I've got the opposite problem and Google doesn't
We've got a problem when trying to generate and use a really large proxy
I've got a question more about a strategy to use in order to implement
Got a problem with Apache and PHP-files of Wordpress. When I go to any
Got a programm with 2 threads. one thread is writing some stuff into the
I've got an about page, and two HTML columns and I want to while
I've got 3 files that relate to this problem. file.h, file.C and user.C. file.h
I've got a page which will have about ten drop down lists which are
I've got a UIToolbar at the top of my iPad app's main screen. It
I've got a problem with Java concurrency. Yes, I looked at questions with almost

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.