POSIX线程同步

时间:2014-08-13 10:15:35

标签: multithreading synchronisation

嗨朋友我在这里遇到了线程同步的问题。我非常愚蠢,这就是为什么寻求你们的帮助。 在我的代码中,我必须从串口读取数据,然后将数据发送到UDP端口。为此,我有两个额外的线程。在第一个线程中,我在无限循环中读取数据,在第二个线程中,我正在进行一些处理,然后发送。

问题是我没有得到buff1 [0]的值,其第二个线程中的积分值为== 1,这意味着我很少得到值== 1。但是在第一个帖子中我不断得到buff1 [0]的值。

我怀疑这是因为我没有使用互斥锁。我想在我的代码中知道如果有必要我将如何使用互斥锁。 Plz指导我改进我的源代码
 以下是我的代码

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "CapSerial.h"
#include <stdlib.h> 
#include<time.h>
//this portion added for thread
#include <pthread.h>
#include "lcd_api.h" 

struct in_addr localInterface;
struct sockaddr_in groupSock; 
void *serial1Thread();
void *serial2Thread();
char buff1[1024];
int sd,min,sec,hr;
int datalen;
char finalbuf[30];
static int serial1_status=0,serial2_status=0;
char serial1[16],sendbuff[16];    
int fd_lcd ,min,sec,hr,ret,secsend,minsend;
struct sockaddr_in servaddr,cliaddr,cliaddr1;


char serial2[16],serial1[16]; 
char sec_buf[3],min_buf[3],hr_buf[3];
char sec_send[3],min_send[3],hr_send[3];
char sec_buf[3],min_buf[3],hr_buf[3];
int sockfd,n,i,sockfd1;
int fd1,fd2,i,ret1,ret2,ret3;



    int main(int argc, char **argv) 
    {                        
     pthread_t thread1,thread2; 
     sockfd = socket(AF_INET,SOCK_DGRAM,0);
     bzero(&cliaddr,sizeof(cliaddr)); 

        cliaddr.sin_family = AF_INET;
        cliaddr.sin_addr.s_addr = inet_addr("10.111.4.100");   
        cliaddr.sin_port = htons(1229);  

        ret1=pthread_create(&thread1,NULL,serial1Thread,NULL);         
        ret3=pthread_create(&thread2,NULL,serial2Thread,NULL); 

        pthread_join(thread1,NULL);                   
        pthread_join(thread2,NULL);

     return 0;
    }





void *serial1Thread()
{


               fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);  
                if (fd1 == -1) 
                {
                perror("open_port: Unable to open /dev/ttyS1 - ");
                return 0;
                } 
                else 
                {
                 fcntl(fd1, F_SETFL, 0);
                }

                 initport(fd1, 9600);
                 printf("baud=%d\n", getbaud(fd1));


        while(1)
        {      

            // Reading Data in infinite while loop 
              if(readport(fd1, buff1))
                         {
                            printf("Read Error happened \n");
                            exit(0);
                         }
                    else
                      {                        
                        printf("\n The value of buff1[0]=%d",buff[0]);     
                      }        

        }


}



         void *serial2Thread()
          {     

           while(1)
           {   

           printf("\n The value of buff1[0]=%d",buff[0]);                
           sendto(sockfd,sendbuff,14,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

           }   

         }

问题是buff1 [0]的值是SOH(标题的开始)其整数 每次在第二个帖子中都没有值== 1。

1 个答案:

答案 0 :(得分:0)

编译器不知道您的变量在多个上下文中使用。为此,您必须使用与atomics,ipc-calls,message queues,mutex或其他任何内容的专用线程同步。

实际的PC运行多个具有多个缓存的内核,大多数数据都保存在寄存器中,依此类推。如果线程之间没有手动同步,则数据将不会同步。

使用volatile无济于事。

所以请阅读有关ipc和线程同步的信息,例如互斥和原子。