小编Mic*_*urr的帖子

使用函数指针从线程调用回调

c程序编译器gcc

我有3个文件.main.c stop_watch.h和stop_watch.c

这个程序确实有效.我叫start_stopwatch.并且在时间到期后它将在main.c timeout_cb()中回调.我也在一个单独的线程中运行它,因为我不想在main中阻塞,因为我将需要运行其他代码.

1)g_start_timer中的秒数总是给垃圾.我以为我可能通过在堆上创建结构来解决这个问题.无论如何我可以解决这个问题.我在考虑在堆上创建秒元素.但是认为这是过度杀戮

2)这个程序运行正常,但是如果我在main printf中注释掉这行("=== timeout_cb:%p \n",timeout_cb); 它将堆叠转储.

3)何时是释放记忆的最佳时间.我正在释放它.但我担心如果在线程完成之前释放内存.这可能会导致非常意外的结果.我想我可以在调用后使用thread_join()作为释放内存.但是,我需要返回在stop_watch.c中创建的thead_id,是否有办法返回在stop_watch.c中创建的thread_id

非常感谢任何建议,

main.c中

/* main.c */
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>

#include "stop_watch.h"

/* call this when the time expires */
void timeout_cb()
{
    printf("=== your time is up run some job here ===\n");
}

int main()
{
    struct data_struct *g_data_struct =
        (struct data_struct*) calloc(1, sizeof(*g_data_struct));

    if(!g_data_struct)
    {
        printf("=== failed to allocate memory ===\n");
        return 0;
    }

    g_data_struct->seconds = 3;
    g_data_struct->func_ptr = timeout_cb;

    //  printf("=== timeout_cb: %p\n", timeout_cb); …
Run Code Online (Sandbox Code Playgroud)

c pointers pthreads function callback

5
推荐指数
1
解决办法
8781
查看次数

Visual Studio 2005 Express Edition是否仍可在任何地方合法使用?

Microsoft网站上的所有链接现在都重定向到2008版.问题是新版本不会与使用2005版本构建的C库链接.

compiler-construction visual-studio-2005 visual-c++

5
推荐指数
1
解决办法
188
查看次数

将链接器脚本文件链接到源代码

我是GNU编译器的新手.

我有一个C源代码文件,其中包含一些结构和变量,我需要在特定位置放置某些变量.

所以,我编写了一个链接器脚本文件,并__ attribute__("SECTION") 在C源代码中使用了 at变量声明.

我正在使用GNU编译器(cygwin)来编译源代码并使用-objcopy选项创建.hex文件,但我没有得到如何在编译时链接我的链接器脚本文件以相应地重定位变量.

我附加了链接描述文件和C源文件以供参考.

请帮助我将链接器脚本文件链接到我的源代码,同时使用GNU创建.hex文件.

/*linker script file*/
/*defining memory regions*/

      MEMORY
      {
         base_table_ram     : org = 0x00700000, len = 0x00000100  /*base table area for BASE table*/ 
         mem2               : org =0x00800200,  len = 0x00000300 /* other structure variables*/
      }
/*Sections directive definitions*/ 

      SECTIONS
      {
        BASE_TABLE    : { }  > base_table_ram

        GROUP                  :
           {
                   .text        : { } { *(SEG_HEADER)  }
                   .data        : { } { *(SEG_HEADER)  }
                   .bss         : { } { *(SEG_HEADER) …
Run Code Online (Sandbox Code Playgroud)

c embedded gnu

5
推荐指数
1
解决办法
5237
查看次数

如何检测正确的函数调用对

我正在寻找一种能够以嵌套方式检测有序函数调用对的工具,如下所示:

f()   // depth 0
   f()  //depth 1
   g()
g()
Run Code Online (Sandbox Code Playgroud)

在每个调用深度处f()必须有一个g()形成函数调用对的调用.这在关键部分进入和退出时尤其重要.

c c++

5
推荐指数
1
解决办法
184
查看次数

从32位寄存器读取16位

我正在尝试从特定寄存器中读取某些值.该手册指定我必须首先访问16位LSB访问,然后访问16位MSB访问.我是否只是一次读取所有32位,然后根据需要分别屏蔽剩余的16 msb/lsb?或者有没有办法只读16位拳.

谢谢,Neco

c embedded assembly arm

5
推荐指数
1
解决办法
2037
查看次数

在MingW下编译简单的pthread代码时链接器错误

我正在尝试编译单个c ++源文件test.cpp,它有一个非常简单的代码,演示了pthread_create(); pthread_cond_signal/pthread_cond_wait()功能.

我在Windows XP上安装了Mingw/Ansys,我正在工作.在MingW提示符中我做:

g++ -IC:/MinGW/include/ -lpthread test.cpp 
//-IC:/MinGW/include to get pthread.h
//-LC:/MinGW/bin to get pthreadGC2.dll
Run Code Online (Sandbox Code Playgroud)

cpp包括pthread.h:

#include <pthread.h>
Run Code Online (Sandbox Code Playgroud)

但这给了我所有pthread库函数的多个链接器未定义的引用错误.

我在这做错了什么.是否可以在Windows上的MingW环境中构建pthread代码?

如何解决这个错误?

C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x23): undefined reference to `_imp__pthread_mutex_lock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x67): undefined reference to `_imp__pthread_cond_signal'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x75): undefined reference to `_imp__pthread_mutex_unlock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x98): undefined reference to `_imp__pthread_exit'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xbc): undefined reference to `_imp__pthread_mutex_lock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xe8): undefined reference to `_imp__pthread_cond_wait'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x10f): undefined reference to `_imp__pthread_mutex_unlock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x135): undefined reference to `_imp__pthread_exit'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x153): undefined reference to `_imp__pthread_attr_init'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x169): undefined reference to `_imp__pthread_mutex_init'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x17f): undefined reference to `_imp__pthread_attr_setdetachstate'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x195): undefined …
Run Code Online (Sandbox Code Playgroud)

mingw pthreads

5
推荐指数
1
解决办法
1万
查看次数

线程fs段寄存器在用户和内核之间切换

fs段寄存器如何指向TEB和KPCR.这些数据结构是保存在线程用户和内核堆栈上的吗?因此,当线程上下文切换从用户到内核发生时,fs段reg包含一个指针,TEB被保存到线程用户堆栈上,然后指向KPCR的内核fs段寄存器被加载回fs段注册?这是fs段寄存器指向TEB和KPCR的方式吗?

windows multithreading kernel

5
推荐指数
1
解决办法
1717
查看次数

如何从命令输出中查找和提取子字符串?

在我详细介绍之前,我想强调一下,使用PowerShell不是一种选择,因为脚本应该在PowerShell可能无法使用的机器上运行.

我需要检查Windows机器上安装的Internet Explorer版本,并根据它的版本对其进行操作.

到目前为止,我的批处理脚本如下所示:

@echo off
setlocal
for /f "tokens=*" %%a in ( 'reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v Version' ) do ( set IEVerReg=%%a )
endlocal
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在reg query为IEVarReg变量分配输出.在不同的Windows版本上,输出是不同的,例如:

Windows XP:

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
    Version     REG_SZ  8.0.6001.18702
Run Code Online (Sandbox Code Playgroud)

Windows 7的:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
    Version    REG_SZ    9.0.8112.16421
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想从版本号中获取第一个数字并执行以下操作:

if
  version == 8
then
  execute this
elseif
  version == 9
then
  execute that
Run Code Online (Sandbox Code Playgroud)

我确定if一旦得到版本号,我就会弄清楚如何做这个部分,但这是我无法弄清楚的版本号部分.我可以在bash,perl等中轻松完成,但不知道Windows命令行.

非常感谢帮助.

PS.我看过Jerold Schulman的剧本,但它返回的版本号对我来说太过分了.

windows command-line batch-file

4
推荐指数
1
解决办法
2万
查看次数

const值与#define,将使用哪种芯片资源?

如果我定义一个宏,或者使用静态const值,在嵌入式系统中,
将使用哪种内存,芯片闪存或芯片内存?哪种方式更好?

c embedded

4
推荐指数
1
解决办法
1813
查看次数

使用在mingw下运行的g ++,__ imp链接错误

我有一个简单的套接字程序,我正在尝试使用在Win8系统上运行mingw(两个最新版本)的g ++进行编译.我收到了常见的链接器错误

undefined reference to `__imp_socket'
undefined reference to `__imp_gethostbyname'
Run Code Online (Sandbox Code Playgroud)

我试过添加-lws2_32而没有运气; 即它仍然找不到参考文献.有人会建议我可能会遗漏的其他东西吗?

这是完整的输出:

G:\source\kak>g++ -o ./test_client -lws2_32 test_client.C
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x4f): undefined reference to `__imp_inet_addr'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x6b): undefined reference to `__imp_socket'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x8b): undefined reference to `__imp_connect'
d:/program files/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o: bad reloc address 0xc in section `.xdata'
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c++ windows mingw imp

4
推荐指数
1
解决办法
4052
查看次数